package com.yunzhi.domain;

import com.alibaba.fastjson.JSONObject;
import com.edais.util.JsonUtil;

import java.util.Collections;
import java.util.List;

/**
 * @author aohujun
 *
 * @param <T>
 */
public class ResultDTO<T> {

	/**
	 * 0000 成功 0001 未登录 0002 未实名 0003 未绑卡 1001 无效签名 1002 yzToken验签失败 1111失败
	 */
	private String resCode;
	/**
	 * 返回消息
	 */
	private String resMsg;
	/**
	 * 返回消息类型 1正常 2重要 3弹框1 4弹框2
	 */
	private String resMsgType;

	/**
	 * 系统时间
	 */
	private long st = System.currentTimeMillis();

	/**
	 * 单个记录
	 */
	private T record;

	/**
	 * 总数
	 */
	private int total;

	/**
	 * 分页记录
	 */
	private List<T> records = Collections.emptyList();

	private int pageSize = 10;

	public ResultDTO(String resCode, String resMsg, String resMsgType, T record) {
		this.resCode = resCode;
		this.resMsg = resMsg;
		this.resMsgType = resMsgType;
		this.record = record;
	}

	public ResultDTO(String resCode, String resMsg, String resMsgType) {
		this.resCode = resCode;
		this.resMsg = resMsg;
		this.resMsgType = resMsgType;
	}

	public ResultDTO(ResultCode resultCode) {
		this.resCode = resultCode.getCode();
		this.resMsg = resultCode.getMsg();
		this.resMsgType = resultCode.getType();
	}

	public ResultDTO(ResultCode resultCode,String msg) {
		this.resCode = resultCode.getCode();
		this.resMsg = msg;
		this.resMsgType = resultCode.getType();
	}

	public ResultDTO(String resCode, String resMsg, String resMsgType, int total, List<T> records) {
		this.resCode = resCode;
		this.resMsg = resMsg;
		this.resMsgType = resMsgType;
		this.total = total;
		this.records = records;
	}

	public ResultDTO(String resCode, String resMsg, String resMsgType, int pageSize, int total, List<T> records) {
		this.resCode = resCode;
		this.resMsg = resMsg;
		this.resMsgType = resMsgType;
		this.pageSize = pageSize;
		this.total = total;
		this.records = records;
	}

	public ResultDTO(ResultCode resultCode, int pageSize, int total, List<T> records) {
		this.resCode = resultCode.getCode();
		this.resMsg = resultCode.getMsg();
		this.resMsgType = resultCode.getType();
		this.pageSize = pageSize;
		this.total = total;
		this.records = records;
	}

	public String getResCode() {
		return resCode;
	}

	public void setResCode(String resCode) {
		this.resCode = resCode;
	}

	public String getResMsg() {
		return resMsg;
	}

	public void setResMsg(String resMsg) {
		this.resMsg = resMsg;
	}

	public String getResMsgType() {
		return resMsgType;
	}

	public void setResMsgType(String resMsgType) {
		this.resMsgType = resMsgType;
	}

	public T getRecord() {
		return record;
	}

	public void setRecord(T record) {
		this.record = record;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPages() {
		if(total % pageSize == 0){
			return total / pageSize;
		}else{
			return total/ pageSize + 1;
		}
	}

	public List<T> getRecords() {
		return records;
	}

	public void setRecords(List<T> records) {
		this.records = records;
	}

	public long getSt() {
		return st;
	}

	public void setSt(long st) {
		this.st = st;
	}

	public String toJsonString(){
		return JSONObject.toJSONString(this);
	}

	public JSONObject toJson(){
		return JSONObject.parseObject(toJsonString());
	}

	public boolean codeEq(ResultCode code) {
		return null != code && code.getCode().equals(resCode);
	}

	public int getRecordSize(){
		return records.size();
	}
}